import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
import { DDSLoader } from "three/examples/jsm/loaders/DDSLoader";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";
import * as THREE from "three";
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
// import vuex from '@/store/index';

export let Manager = new THREE.LoadingManager();

let loaderGLTF = new GLTFLoader(Manager);
let DRACOloading = new DRACOLoader();
// DRACOloading.setDecoderPath("/static/draco/");
DRACOloading.setDecoderPath(window.Vue.$store.state.threeUrlprefix+"/static/draco/");
loaderGLTF.setDRACOLoader(DRACOloading);
loaderGLTF.setDDSLoader(new DDSLoader());
// DRACOloading.preload();
let loaderFBX = new FBXLoader(Manager);

export function loadHDR(url, renderer) {
  // HDR贴图
  return new Promise((resolve) => {
    let texload = new RGBELoader();
    const pmremGenerator = new THREE.PMREMGenerator(renderer);
    pmremGenerator.compileEquirectangularShader();
    texload.load(url, (textures) => {
      console.log("hdr");
      // textures.encoding = THREE.RGBEEncoding; //色彩映射
      const envMap = pmremGenerator.fromEquirectangular(textures).texture;
      textures.dispose();
      resolve(envMap);
    });
  });
}
export function loadPNG(src) {
  return new THREE.TextureLoader().load(src);
}
export function loadCube() {
  // 加载天空盒
  return new Promise((resolve) => {
    let texture = new THREE.CubeTextureLoader().load([
      "sofa.jpg",
      "sofa.jpg",
      "sofa.jpg",
      "sofa.jpg",
      "sofa.jpg",
      "sofa.jpg",
    ]);
    resolve(texture);
  });
}
export function loadGLTF(url) {
  // 加载GLTF
  return new Promise((resolve) => {
    console.log('加载GLTF',url)
    loaderGLTF.load(`${url}`, (mesh) => {
      resolve(mesh);
      // resolve(mesh.scene);
    });
  });
}
// export function loadFBX(url) {
//   // 加载FBX
//   return new Promise((resolve) => {
//     console.log('加载fbx',url);
//     loaderFBX.load(`${url}`, (mesh) => {
//       resolve(mesh);
//     });
//   });
// }

//根据物体大小调整模型缩放比例 位置调整
export function setOBJPos(obj,RulerArr) {
  let boxSizeObj = new THREE.Box3()
    .setFromObject(obj)
    .getSize(new THREE.Vector3());
  let maxBoxSize = Math.max(boxSizeObj.x, boxSizeObj.y, boxSizeObj.z);
  let sizeNum = maxBoxSize>2 ? 1.4:1
  obj.scale.set(
    sizeNum / maxBoxSize,
    sizeNum / maxBoxSize,
    sizeNum / maxBoxSize
  );
  // 物体位置略微向下移动
  // -boxSizeObj.y / 2
  obj.position.set(0, 0, 0);

  //-----------------------------------------------------------------------------

  getRuler(boxSizeObj, obj,RulerArr);

}

export function getListArr (array){
  if(array){
   for (var t = 0; t < array.length; t++) {
      array[t].MeshNum = ~~array[t].name.split('Mesh_')[1]
  　　if (array[t].typeList.length==0||array[t].isShow == 'hidden'||array[t].isShow == false) {
          array.splice(t, 1); 
  　　　　t--; 
  　　}
    }
    return quickSort (array);
  }else{
    return array;
  }

}
export function quickSort (array)  {
  if (array.length < 2) return array;
  let leftArray = [];
  let rightArray = [];
  let baseDigit = array[0];
  array.forEach(element => {
      if (element.MeshNum < baseDigit.MeshNum) {
          leftArray.push(element);
      } else if (element.MeshNum > baseDigit.MeshNum) {
          rightArray.push(element);
      }
  });
  return quickSort(leftArray).concat(baseDigit, quickSort(rightArray))
};

function onProgress(xhr) {
  console.log(xhr)
  let progress = Math.floor((xhr.loaded / xhr.total) * 100);
  // progress = progress >= 95 ? 95 : progress;
  // vuex.state.loadingModelProgress = progress
}
function onError(e) { console.log(e); }
